﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.member;
using System.Web.Security;
namespace Zebra.Umbraco.Marcos.Survey
{
    public partial class surveyList : LoginRequiredControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                var xx = from c in db.surveySurveys
                         where c.nodeId == int.Parse(Membership.GetUser().ProviderUserKey.ToString())
                         select c;
                if (xx.Count<surveySurvey>() > 0)
                {
                    GridView1.DataSource = xx;
                    GridView1.DataBind();
                    GridView1.Visible = true;
                    lbNoRecord.Visible = false;
                }
                else
                {
                    GridView1.Visible = false;
                    lbNoRecord.Visible = true;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                surveySurvey survey = new surveySurvey()
                {

                    nodeId = int.Parse(Membership.GetUser().ProviderUserKey.ToString()),
                    name = txtSurveyName.Text.Trim(),
                    type = int.Parse(DropDownList1.SelectedValue),
                    RowCreatedOn = DateTime.Now,
                    RowModifiedOn = DateTime.Now
                };
                db.surveySurveys.InsertOnSubmit(survey);
                db.SubmitChanges();
            }
            BindData();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "remove")
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    db.ExecuteCommand(string.Format("delete [surveyQuestion] where [surveyID] = {0}", e.CommandArgument));
                    db.ExecuteCommand(string.Format("delete [surveySurvey] where [surveyID] = {0}", e.CommandArgument));
                }
            }
            Response.Redirect("survey.aspx");
        }
    }
}